package com.hamilton.erik.pt.mobile.android.app;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;

import com.hamilton.erik.pt.mobile.android.R;

public class DoublePickerDialog extends AlertDialog implements 
			OnClickListener, OnValueChangeListener {
	
	public interface OnDoubleSetListener {
		void onDoubleSet(Double value);
	}
	
	private final static String WHOLE = "whole";
	private final static String TENTHS = "tenths";
	private final static String HUNDRETHS = "hundreths";
	
	private OnDoubleSetListener callBack;
	
	private NumberPicker wholeNumberPicker;
	private NumberPicker tenthsPicker;
	private NumberPicker hundrethsPicker;
	
	public DoublePickerDialog(Context context, OnDoubleSetListener callBack) {
		super(context);

		this.callBack = callBack;
		
		setButton(context.getText(R.string.set_button_text), this);
		setButton2(context.getText(R.string.cancel_button_text),(OnClickListener)null);
		
		LayoutInflater inflater = 
				(LayoutInflater) context.getSystemService(
											Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.double_picker_dialog, null);
		setView(view);

		wholeNumberPicker = (NumberPicker)view.findViewById(R.id.whole_number_picker);
		wholeNumberPicker.setOnValueChangedListener(this);
		wholeNumberPicker.setMaxValue(Integer.MAX_VALUE);
		wholeNumberPicker.setMinValue(0);
		
		tenthsPicker = (NumberPicker)view.findViewById(R.id.tenths_picker);
		tenthsPicker.setOnValueChangedListener(this);
		tenthsPicker.setMaxValue(9);
		tenthsPicker.setMinValue(0);
		
		hundrethsPicker = (NumberPicker)view.findViewById(R.id.hundreths_picker);
		hundrethsPicker.setOnValueChangedListener(this);
		hundrethsPicker.setMaxValue(9);
		hundrethsPicker.setMinValue(0);
		
		setValue(0.0);
		
		updateTitle();
	}
	
	public void setValue(Double value) {
		String[] wholeAndDecimal = value.toString().split("\\.");
		
		// Always going to have at least the whole number
		wholeNumberPicker.setValue(Integer.parseInt(wholeAndDecimal[0]));
		
		if (wholeAndDecimal.length > 1) {
			// Extract the tenths and hundreths
			if(wholeAndDecimal[1].length() == 1) {
				// Only have tenths
				tenthsPicker.setValue(Character.getNumericValue(
										wholeAndDecimal[1].charAt(0)));
				hundrethsPicker.setValue(0);
			}
			else {
				tenthsPicker.setValue(Character.getNumericValue(
						wholeAndDecimal[1].charAt(0)));
				hundrethsPicker.setValue(Character.getNumericValue(
						wholeAndDecimal[1].charAt(1)));
			}
		}
		
		updateTitle();
	}
	
	public void setMaxValue(int max) {
		wholeNumberPicker.setMaxValue(max);
	}
	
	public void setMinValue(int min) {
		if(min < 0) {
			min = 0;
		}
		
		wholeNumberPicker.setMinValue(min);
	}
	
	public void onClick(DialogInterface dialog, int which) {
		if(callBack != null) {
			wholeNumberPicker.clearFocus();
			tenthsPicker.clearFocus();
			hundrethsPicker.clearFocus();
			callBack.onDoubleSet(getValue());
		}
	}
		
	@Override
	public Bundle onSaveInstanceState() {
		Bundle state = super.onSaveInstanceState();
		state.putInt(WHOLE, wholeNumberPicker.getValue());
		state.putInt(TENTHS, tenthsPicker.getValue());
		state.putInt(HUNDRETHS, hundrethsPicker.getValue());
		return state;
	}
	
	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		
		wholeNumberPicker.setValue(savedInstanceState.getInt(WHOLE));
		tenthsPicker.setValue(savedInstanceState.getInt(TENTHS));
		hundrethsPicker.setValue(savedInstanceState.getInt(HUNDRETHS));
	}

	@Override
	public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
		if(wholeNumberPicker.getValue() == wholeNumberPicker.getMaxValue()) {
			tenthsPicker.setValue(0);
			hundrethsPicker.setValue(0);
		}
		updateTitle();
	}
	
	private Double getValue() {
		String valueString = String.format("%d.%d%d",
								wholeNumberPicker.getValue(),
								tenthsPicker.getValue(),
								hundrethsPicker.getValue());
		
		return Double.parseDouble(valueString);
	}
	
	private void updateTitle() {
		setTitle(String.format("%d.%d%d",
								wholeNumberPicker.getValue(),
								tenthsPicker.getValue(),
								hundrethsPicker.getValue()));
	}
}
